package com.coderxq.blog.web.service.role.impl;

import cn.hutool.core.util.IdUtil;
import com.coderxq.blog.exception.WsException;
import com.coderxq.blog.web.dao.RoleMapper;
import com.coderxq.blog.web.entity.dto.RoleMenuList;
import com.coderxq.blog.web.entity.pojo.Role;
import com.coderxq.blog.web.entity.pojo.RoleMenu;
import com.coderxq.blog.web.entity.pojo.User;
import com.coderxq.blog.web.entity.vo.RoleInfoVo;
import com.coderxq.blog.web.service.menu.impl.MenuServiceImpl;
import com.coderxq.blog.web.service.role.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;

/**
 * @Author: lijingming
 * @Date: 2025/2/15
 * @Description:
 **/
@Component
public class RoleServiceImpl  implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuServiceImpl menuService;

    @Override
    public int insertRole(Role role) {
        String snowflakeId = IdUtil.getSnowflakeNextIdStr();
        if(role!=null){
            role.setRoleId(snowflakeId);
            Integer result =  roleMapper.insertRole(role);
            return result;
        }
        return 0;
    }

    @Override
    public int deleteRole(String roleId) {
        Integer result = roleMapper.deleteRole(roleId);
        return result;
    }

    @Override
    public Role updateRole(String roleId, String roleName) {
        Role updateRole=new Role();
        try{
            roleMapper.updateRole(roleId,roleName);
            updateRole =findRoleById(roleId);

        }catch (Exception e){
            throw  new WsException(e.getMessage());
        }
        return updateRole;
    }

    @Override
    public PageInfo<Role> findAllRoleList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<Role> roleList = roleMapper.getAllRoleList();
        return  new PageInfo<>(roleList);
    }

    @Override
    public Role findRoleById(String roleId) {
        Role role = roleMapper.findRoleById(roleId);
        return role;
    }

    @Override
    public int insertRoleMenu(RoleMenu roleMenu) {
        Integer result = roleMapper.insertRoleMenu(roleMenu);
        return result;
    }


    //重置角色菜单
    @Override
    public int resetRoleMenuList(RoleMenuList roleMenus) {

      try{
          roleMapper.deleteRoleAllMenu(roleMenus.getRoleId());
          Integer result= roleMapper.insertRoleMenus(roleMenus);
          return result;
      }catch (Exception e){
          throw  new WsException(e.getMessage());
      }

    }

    @Override
    public int deleteRoleMenu(String roleId, String menuId) {
        Integer result = roleMapper.deleteRoleMenu(roleId,menuId);
        return result;
    }

    @Override
    public RoleInfoVo findRoleMenuList(String roleId) {
        RoleInfoVo roleInfo = roleMapper.findRoleMenuList(roleId);
        roleInfo.setTreeMenuList(menuService.formatMenu(roleInfo.getMenuList())); ;

        return roleInfo;
    }
}
